{
 "metadata": {
  "signature": "sha256:a2521d7393f7e36e5eba1247c802cf3624336de02c3a421ef53c7f5ed329e631"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Stride tricks for the Game of Life\n",
      "==================================\n",
      "\n",
      "This is similar to [:../SegmentAxis:Segment axis], but for 2D arrays\n",
      "with 2D windows.\n",
      "\n",
      "The Game of Life is a cellular automaton devised by the British\n",
      "mathematician John Horton Conway in 1970, see [1].\n",
      "\n",
      "It consists of a rectangular grid of cells which are either dead or\n",
      "alive, and a transition rule for updating the cells' state. To update\n",
      "each cell in the grid, the state of the 8 neighbouring cells needs to be\n",
      "examined, i.e. it would be desirable to have an easy way of accessing\n",
      "the 8 neighbours of all the cells at once without making unnecessary\n",
      "copies. The code snippet below shows how to use the devious stride\n",
      "tricks for that purpose.\n",
      "\n",
      "[1] [Game of Life](http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)\n",
      "at Wikipedia"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "from numpy.lib import stride_tricks\n",
      "x = np.arange(20).reshape([4, 5])\n",
      "xx = stride_tricks.as_strided(x, shape=(2, 3, 3, 3), strides=x.strides + x.strides)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "array([[ 0,  1,  2,  3,  4],\n",
        "       [ 5,  6,  7,  8,  9],\n",
        "       [10, 11, 12, 13, 14],\n",
        "       [15, 16, 17, 18, 19]])"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "xx"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "array([[[[ 0,  1,  2],\n",
        "         [ 5,  6,  7],\n",
        "         [10, 11, 12]],\n",
        "\n",
        "        [[ 1,  2,  3],\n",
        "         [ 6,  7,  8],\n",
        "         [11, 12, 13]],\n",
        "\n",
        "        [[ 2,  3,  4],\n",
        "         [ 7,  8,  9],\n",
        "         [12, 13, 14]]],\n",
        "\n",
        "\n",
        "       [[[ 5,  6,  7],\n",
        "         [10, 11, 12],\n",
        "         [15, 16, 17]],\n",
        "\n",
        "        [[ 6,  7,  8],\n",
        "         [11, 12, 13],\n",
        "         [16, 17, 18]],\n",
        "\n",
        "        [[ 7,  8,  9],\n",
        "         [12, 13, 14],\n",
        "         [17, 18, 19]]]])"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "xx[0,0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "array([[ 0,  1,  2],\n",
        "       [ 5,  6,  7],\n",
        "       [10, 11, 12]])"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "xx[1,2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "array([[ 7,  8,  9],\n",
        "       [12, 13, 14],\n",
        "       [17, 18, 19]])"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x.strides"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "(40, 8)"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "xx.strides"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "(40, 8, 40, 8)"
       ]
      }
     ],
     "prompt_number": 14
    }
   ],
   "metadata": {}
  }
 ]
}